package com.meowcook.utils;


import cn.hutool.jwt.JWT;

import java.util.Date;

public class JwtUtil {
    private static final byte[] key = "xiaozhong".getBytes();
    private static final long expire = 30 * 1000;


    /**
     * 生成登录用的JWT令牌
     * @param username 用户名
     * @return JWT令牌
     */
    public static String createJWT(String username) {
        return JWT.create()
                .setPayload("username", username)
                .setExpiresAt(new Date(System.currentTimeMillis() + expire))
                .setKey(key)
                .sign();
    }

    /**
     * 验证JWT令牌
     * @param jwt JWT令牌
     * @return 通过返回true，否则返回false
     */
    public static boolean verify(String jwt) {

        return JWT.of(jwt).setKey(key).validate(0);
    }
}
